#!/usr/bin/env python3
"""
快速测试BPR修复效果的脚本
在服务器上运行此脚本验证修复
"""

import torch
import torch.nn.functional as F


def quick_bpr_test():
    """快速BPR测试"""
    print("快速BPR修复测试")
    print("=" * 30)

    # 模拟得分差
    diff = torch.tensor([0.0, 1.0, -1.0, 10.0, -10.0])

    # 原始方法（有数值问题）
    old_loss = -torch.log(torch.sigmoid(diff) + 1e-10)

    # 修复方法
    new_loss = F.softplus(-diff)

    print("得分差:", diff.tolist())
    print("原始损失:", [f"{x:.4f}" for x in old_loss.tolist()])
    print("修复损失:", [f"{x:.4f}" for x in new_loss.tolist()])

    # 检查当diff=0时的损失
    zero_diff = torch.tensor([0.0])
    zero_old = -torch.log(torch.sigmoid(zero_diff) + 1e-10)
    zero_new = F.softplus(-zero_diff)

    print(f"\n当得分差=0时:")
    print(f"  原始损失: {zero_old.item():.4f}")
    print(f"  修复损失: {zero_new.item():.4f}")
    print(f"  理论值(ln2): {torch.log(torch.tensor(2.0)).item():.4f}")

    print("\n✅ 修复验证完成")


if __name__ == "__main__":
    quick_bpr_test()